#ifndef __DB_CONNECTION_H__
#define __DB_CONNECTION_H__

#include <string>
#include <stdint.h>

#include "mysqlpp/mysql++.h"
#include "mysqlpp/ssqls.h"
#include "ctTime.h"
#include "Zebra.h"

class DBConnection {
public:
        typedef mysqlpp::Connection             Connection;
        typedef mysqlpp::Query                        Query;
        typedef mysqlpp::StoreQueryResult       StoreQueryResult;
public:
        DBConnection();
        virtual ~DBConnection();

        int32_t init(std::string db, std::string user, std::string host, std::string passwd);

        void release();

        int32_t connect();
        void disconnect();
        int32_t reconnect();

  std::string const getDB() {
          return _database;
        }

        std::string const getHost() {
                return _host;
        }

        std::string const getUser() {
                return _user;
        }

        std::string const getPassword() {
                return _password;
        }

        Connection * getConn() {
                if(_life_time.elapse()>600) {
                  Zebra::logger->trace("mysql connection will be reconnected..");
                        reconnect();
                }
                return _conn;
        }
private:
        std::string _database;
        std::string _host;
        std::string _user;
        std::string _password;
private:
        mysqlpp::Connection* _conn;
  ctTime _life_time;
};

#endif // __DB_CONNECTION_H__
